package tags;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

public class NavigationTag extends TagSupport
{
	private static final long serialVersionUID = -7100487460755147184L;
	private PageContext pageContext = null;
	private Tag parent = null;
	
	private HashMap<String, String> links;
	private Set<String> keys;
	private Iterator<String> i;
	
	private String temp = "";
	
	public String link;
	public String var;
	
	public void setLinks(HashMap<String, String> l)
	{
		//System.out.println("NavigationTag.setLinks()");
		links = l;
		keys = links.keySet();
		i = keys.iterator();
	}
	
	public void setVar(String v)
	{
		var = v;
	}
	
	public int doAfterBody() throws JspException
	{
		//System.out.println("doAfterBody()");
		if(i.hasNext())
		{
			temp = (String)i.next();
			link = "<a href=\"" + links.get(temp) + "\">" + temp + "</a>";
			pageContext.setAttribute( var, link );
			return EVAL_BODY_AGAIN;
		}
		return SKIP_BODY;
	}

	@Override
	public int doEndTag() throws JspException
	{
		//System.out.println("doEndTag()");
		return EVAL_PAGE;
	}
	
	@Override
	public int doStartTag() throws JspException
	{
		//System.out.println("NavigationTag.doStartTag()");
		if(i.hasNext())
		{
			temp = (String)i.next();
			link = "<a href=\"" + links.get(temp) + "\">" + temp + "</a>";
			pageContext.setAttribute( var, link );
			return EVAL_BODY_INCLUDE;
		}
		
		return SKIP_BODY;
	}
	
	@Override
	public Tag getParent()
	{
		return parent;
	}
	
	@Override
	public void release()
	{
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void setPageContext( PageContext pc )
	{
		pageContext = pc;
	}
	
	@Override
	public void setParent( Tag t )
	{
		parent = t;
	}
}
